package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Library {

    private int totalNumberOfBooks = 0;
    private List<Book> allBooks = new ArrayList<Book>();
    private List<Book> availableBooks = new ArrayList<Book>();

    public int totalNumberOfBooks() {
        return allBooks.size();
    }

    public int numberOfAvailableBooks() {
        return availableBooks.size();
    }

    public void addBooks(Book... booksToAdd) {
        for(Book book : Arrays.asList(booksToAdd)) {
            allBooks.add(book);
            availableBooks.add(book);
        }
    }

    public List<Book> allBooks() {
        return allBooks;
    }

    public List<Book> availableBooks() {
        return availableBooks;
    }

    public boolean canLoan(Book theBook) {
        return availableBooks.contains(theBook);
    }

    public void loanBook(Book theBook) {
        availableBooks.remove(theBook);
    }

    public boolean canReceive(Book theBook) {
        return !availableBooks.contains(theBook) && allBooks.contains(theBook);
    }

    public void receiveBook(Book theBook) {
        if(canReceive(theBook)) {
            availableBooks.add(theBook);
        }
    }

    public List<Book> getAvailableBooksByTitle(String title) {
        List<Book> results = new ArrayList<Book>();

        for(Book book : availableBooks) {
            if(book.getTitle().equals(title)) results.add(book);
        }
        return results;
    }

}
